import jwt from 'jsonwebtoken';

/**
 * 生成token
 * @param {Object} payload
 * @param {string} secret
 * @param {string | number} token_time
 * @returns {Promise}
 */
export const generateToken = (payload: object, secret: string, token_time?: string | number): Promise<string> => new Promise((resolve, reject) => {
  jwt.sign(payload, secret, {
    expiresIn: token_time,
  }, (err, token) => {
    if (err) {
      reject(err);
    } else {
      resolve(token as string);
    }
  });
});
